package cn.lanqiao.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.lanqiao.dao.ShowFriend;
import cn.lanqiao.util.JdbcUtil;

public class ShowFriendImpl implements ShowFriend{
	@Override
	public Object[][] display(String id) throws SQLException {  //通过用户id查询当前账号的好友信息
		Object[][] datas = null;
		Connection conn = JdbcUtil.getConnection();  //建立数据库连接对象
		String sql = "select * from k_friend where id=?";
		PreparedStatement ps = null;  //创建statement对象，用于执行sql命令
		ResultSet rs = null;	//通过ResultSet接收查询结果
		ps = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
		ps.setString(1, id);
		rs=ps.executeQuery();
		int temp = 0;	//临时变量temp，用于取得查询结果的数量	
		while(rs.next()) {
			temp++;
		}
		datas = new Object[temp][];	//根据查询结果的行数来确定二维数组的大小
		rs.beforeFirst();
		for(int i=0;i<temp;i++) {
			if(rs.next()) {
				String friendname = rs.getString("friendname");
				//将查询结果存储到二维数组中
				datas[i] = new Object[] {friendname};
			}
		}
		rs.close();
		ps.close();
		conn.close();
		return datas;
	
	}
	
}
